﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text;
using System.Data;

namespace Jy123.Web.home.MiniContent
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Gift : IHttpHandler
    {
        private int userID;
        public void ProcessRequest(HttpContext context)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            string strUserGuestID = context.Request.QueryString["userID"];
            if (!string.IsNullOrEmpty(strUserGuestID))
            {
                userID = Convert.ToInt32(strUserGuestID);
            }

            StringBuilder sbHtml = new StringBuilder(context.Cache["user_minicontent"].ToString());//sbContent

            string strUserID = context.Request.QueryString["MiniContentUserID"];
            string strGiftUl = string.Empty;
            if (!string.IsNullOrEmpty(strUserID))
            {
                strUserID = strUserID.Substring(0, strUserID.IndexOf("?"));
                strGiftUl = GiftListother(context, Convert.ToInt32(strUserID));
                
            }
            else
            {
                strGiftUl = UserListForGift(context, userID);
            }
            if (strGiftUl.Trim() == "")
            {
                strGiftUl = "暂无动态!";
            }
            sbHtml = sbHtml.Replace("$ContentMini_ul$", strGiftUl);
            context.Response.ContentType = "text/plain";
            context.Response.Write(sbHtml.ToString());
        }

        /// <summary>
        /// 获取礼物列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="uid">用户ID</param>
        /// <returns>字符串</returns>
        private string GetGiftList(HttpContext context, string uid)
        {
            List<int?> count;
            StringBuilder sbGiftList = new StringBuilder();
            string cacheGiftList = context.Cache["user_minicontent_ul_gift_a"].ToString();
            DataTable dtGiftList = new BLL.User.Gift().GiftRecord(1, int.MaxValue, "FReciever='" + uid + "'",out count).Tables[0];
            for (int i = 0; i < dtGiftList.Rows.Count; i++)
            {
                string strGiftList = cacheGiftList;
                strGiftList = strGiftList.Replace("$Title$", dtGiftList.Rows[i]["Decription"].ToString());

                string strPic = dtGiftList.Rows[i]["Picture"].ToString();
                bool blPic = new AppCode.CreateDir().EqualWH(strPic, 45, 45);
                if (blPic) { strGiftList = strGiftList.Replace("$FilePath$", new AppCode.CreateDir().ReDraw(45, 0, strPic)); }
                else { strGiftList = strGiftList.Replace("$FilePath$", strPic); }

                sbGiftList.Append(strGiftList);
            }
            return sbGiftList.ToString();
        }
        /// <summary>
        /// 获取用户列表(好友)
        /// </summary>
        /// <param name="context"></param>
        /// <param name="userID">当前用户ID</param>
        /// <returns>字符串</returns>
        private string UserListForGift(HttpContext context, int userID)
        {
            StringBuilder sbUlGift = new StringBuilder();
            string cacheUlGift = context.Cache["user_minicontent_gift_ul"].ToString();
            DataTable dtFriendGiftRecord = new Jy123.BLL.User.Gift().GiftRecordFriendTop1(userID, 1, int.MaxValue, string.Empty).Tables[0];
            for (int i = 0; i < dtFriendGiftRecord.Rows.Count; i++)
            {
                string str = cacheUlGift;
                //根据用户ID判断用户是否有头像,如果没有头像就返回默认头像地址
                string userPortrait = new Jy123.BLL.User.SPace().DefaultPortrait(Convert.ToInt32(dtFriendGiftRecord.Rows[i]["FReciever"]));

                str = str.Replace("$UserID$", dtFriendGiftRecord.Rows[i]["FReciever"].ToString());
                str = str.Replace("$UserName$", dtFriendGiftRecord.Rows[i]["FRecieverName"].ToString());
                str = str.Replace("$Portrait$", userPortrait);
                str = str.Replace("$GMessage$", dtFriendGiftRecord.Rows[i]["GMessage"].ToString());
                str = str.Replace("$GName$", dtFriendGiftRecord.Rows[i]["GName"].ToString());
                str = str.Replace("$Picture$", dtFriendGiftRecord.Rows[i]["Picture"].ToString());
                sbUlGift.Append(str);
            }
            return sbUlGift.ToString();
        }
        /// <summary>
        /// 获取礼物列表(访问别人时)
        /// </summary>
        /// <param name="context"></param>
        /// <param name="userID">用户ID</param>
        /// <returns>字符串</returns>
        private string GiftListother(HttpContext context, int userID)
        {
            List<int?> count;
            string strReceiveList = context.Cache["user_minicontent_ul_gift_other"].ToString();
            StringBuilder sbReceiveList = new StringBuilder();
            DataTable dtReceiveList = new Jy123.BLL.User.Gift().GiftRecord(1, 24, "FReciever='" + userID + "'",out count ).Tables[0];
            for (int i = 0; i < dtReceiveList.Rows.Count; i++)
            {
                string str = strReceiveList;
                str = str.Replace("$Picture$", dtReceiveList.Rows[i]["Picture"].ToString())
                    .Replace("$GName$", dtReceiveList.Rows[i]["GName"].ToString())
                    .Replace("$GMessage$", dtReceiveList.Rows[i]["GMessage"].ToString())
                    .Replace("$FUser$", dtReceiveList.Rows[i]["FUser"].ToString())
                    .Replace("$FUserName$", dtReceiveList.Rows[i]["FUserName"].ToString())
                    .Replace("$UserID$",userID.ToString());

                    //.Replace("$RecordID$", dtReceiveList.Rows[i]["ID"].ToString());
                //string privacy = dtReceiveList.Rows[i]["Privacy"].ToString();
                //switch (privacy)
                //{
                //    case "0": privacy = "公开"; break;
                //    case "1": privacy = "网络"; break;
                //    case "2": privacy = "好友"; break;
                //    case "3": privacy = "自己"; break;
                //    default: privacy = "公开"; break;
                //}
                //str = str.Replace("$Privacy$", privacy);
                sbReceiveList.Append(str);
            }
            return sbReceiveList.ToString();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
